//
//  HelpListViewController.m
//  SafeFoodManagerDemo
//
//  Created by ios-1 on 2018/3/13.
//  Copyright © 2018年 bob. All rights reserved.
//

#import "HelpListViewController.h"
#import "HelpListTableViewCell.h"
#import "GetRegisterInforModel.h"
#import "AddRecruitViewController.h"
#import "HelpDetailViewController.h"
#import "HelpListModel.h"
@interface HelpListViewController ()
@property(nonatomic,copy)NSString*SearchKeyWord;
@property(nonatomic,copy)NSString*positionId;      //当前岗位ID
@property(nonatomic,copy)NSString*shopId;          //当前岗位商户ID
@property(nonatomic,assign)BOOL is_selectPosition; //是否点击当前岗位
@end

@implementation HelpListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.homeTableView.estimatedRowHeight=70;
    [self.homeTableView registerNib:[UINib nibWithNibName:@"HelpListTableViewCell" bundle:nil] forCellReuseIdentifier:@"HelpCell"];
    [self.homeTableView.mj_header beginRefreshing];
}

#pragma mark-tablevie代理方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HelpListData *data=self.dataArr[indexPath.row];
    
    HelpListTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"HelpCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLabel.text = data.name;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HelpListData *data=self.dataArr[indexPath.row];
    
    HelpDetailViewController *Infor=[HelpDetailViewController new];
    
    Infor.reqUrl=@"/index.php/FoodCheckApi/additiveDetail.json";
    
    Infor.params=@{@"id":[NSString stringWithFormat:@"%@",data.ID]};
    
    Infor.title=@"详情";
    
    [self.navigationController pushViewController:Infor animated:YES];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}

-(void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
    
    view.tintColor=TBColorTableViewBg;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 0.1;
    
}

#pragma mark-搜索跳转事件
-(void)PushChangeStatueVC
{
    BOOL isSelf=self.Reqmodel.shopId==0?YES:NO;
    
    BaseSearchChooseViewController *Choose=[[BaseSearchChooseViewController alloc]init];

    if (isSelf) {
        
        if ([self.Reqmodel.ptype isEqualToString:@"1"]) {
            
            Choose.CustomArr=[SearchAndChooseManager GetChooseDateWithType:AdditiveListType andIsLookSelf:isSelf];
            
        }else if ([self.Reqmodel.ptype isEqualToString:@"2"])
        {
            
            NSArray *PostArr= [USER_DEFAULT objectForKey:@"TBChoose"];//如果本地存在则读取本地的
            
            if (PostArr.count!=0) {
                
                Choose.CustomArr=PostArr;
                
            }else{
                
                Choose.CustomArr=@[@{@"fieldName":@"marketIds",@"name":@"开办方",@"value":@""},@{@"fieldName":@"type",@"name":@"",@"value":@""},@{@"fieldName":@"checkUserIds",@"name":@"检查员",@"value":@""},@{@"fieldName":@"startDate",@"name":@"开始日期",@"value":@""},@{@"fieldName":@"endDate",@"name":@"结束日期",@"value":@""}];
            }
        }
        
    }else{
        
        Choose.CustomArr=[SearchAndChooseManager GetChooseDateWithType:PatrolListType andIsLookSelf:isSelf];
    }
    Choose.SearchPlace=self.searchName;
    
    Choose.KeyWord=self.SearchKeyWord;
    
    Choose.Lshopid=self.Reqmodel.shopId;
    
    Choose.SureBlock=^(NSString *paras,NSString*keyword)
    {
        self.Reqmodel.params=paras;
        
        self.SearchKeyWord=keyword;
        
        [self.homeTableView.mj_header beginRefreshing];
    };
    
    [self.navigationController pushViewController:Choose animated:YES];
}

#pragma mark-空白页点击事件
- (void)actionTapEmptyDataSetNodataView
{
    [self.homeTableView.mj_header beginRefreshing];
}

/**
 *  MJ头部刷新(封装层方法, 需要子类继承或者重写)
 */
- (void)actionMJHeaderRefresh
{
    self.Reqmodel.currentPage=1;
    
    [self.homeTableView.mj_footer resetNoMoreData];
    
    [self loadDatas];
}

/**
 *  MJ底部加载(封装方法, 需要子类继承或者重写)
 */
- (void)actionMJFooterLoadMore
{
    self.Reqmodel.currentPage++;
    
    [self loadDatas];
}

-(void)loadDatas
{
    [self showLoadingAnimation];
    
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    [params setObject:@"1" forKey:@"pageOn"];
//    [params setObject:@"50" forKey:@"pageSize"];
//    [params setObject:[NSString stringWithFormat:@"%ld",self.Reqmodel.currentPage] forKey:@"currentPage"];
    
    BaseReqApi *api=[[BaseReqApi alloc]initWithRequestUrl:@"/index.php/FoodCheckApi/additiveList.json" andrequestTime:5 andParams:self.Reqmodel.mj_keyValues andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        [self stopLoadingAnimation];
        
        HelpListModel *InforModel=[HelpListModel mj_objectWithKeyValues:responseObject];
        
        if (responseStatus==1) {
            //请求成功
            
             self.TotalNum=[NSString stringWithFormat:@"一共找到%ld个%@",[responseObject[@"totalRecord"]integerValue],@"记录"];
            if (InforModel.data.count==0&&self.Reqmodel.currentPage==1) {
                
                [self.dataArr removeAllObjects];
                
                self.Type=TBNODateType;
                
                [self.homeTableView reloadData];
                
            }else if (InforModel.data.count==0&&self.Reqmodel.currentPage!=1){
                
                self.btn.hidden=NO;
                [self.homeTableView.mj_footer endRefreshingWithNoMoreData];
                
            } else{
                
                self.btn.hidden=NO;
                
                if (self.Reqmodel.currentPage==1) {
                    
                    [self.dataArr removeAllObjects];
                    
                }
                
                [self.dataArr addObjectsFromArray:InforModel.data];
                
                [self.homeTableView reloadData];
                
            }
            
            [self NetRequestSuccess:self scrollView:self.homeTableView];
            
        }else if (responseStatus==0||responseStatus==2)
        {
            if (self.Reqmodel.currentPage>1) {
                
                self.Reqmodel.currentPage=self.Reqmodel.currentPage-1;
            }
            
            [self NetRequestFail:self scrollView:self.homeTableView msg:message];
            
            self.Type=TBReqFalie;
            
        }else{
            //404需要重新登录了
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
        }
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
